#include "VDStrings.h"

#include <QFile>
#include <QTextStream>
#include <QTime>

#include "VDUser.h"
#include "VDResource.h"

QString VDStr::s_tipUser;
bool VDStr::s_isTipUser = false;

QString VDStr::s_msgMaster;
QString VDStr::s_msgUser;
bool VDStr::s_isMsgMaster = false;
bool VDStr::s_isMsgUser = false;

QString VDStr::s_labelUser;
bool VDStr::s_isLabelUser = false;

const char* pathData = "data/";
const char* pathTmpl = "data/strings_tmpl/";

const char* replImgSrc  = "img src='";
const char* replImgDest = "img src='data/strings_tmpl/";

const char* replBGSrc  = "background='";
const char* replBGDest = "background='data/strings_tmpl/";

QString readFile(const char* nameFile)
{
	QString str;
	QFile file(QObject::tr(pathTmpl) + nameFile);
	if (file.open(QIODevice::ReadOnly | QIODevice::Text))
	{
		QTextStream in(&file);
		str = in.readAll();
	}

	return str;
}

const QString& VDStr::tipUser()
{
	if(!s_isTipUser)
	{
		s_isTipUser = true;
		s_tipUser = readFile("tip_user.html");
		if (s_tipUser.isEmpty())
			s_tipUser = "<img src='%1'><br>%2<br>%3<br>%4";
	}

	return s_tipUser;
}

QString VDStr::tipUser( const VDUser& u)
{
	QString s4 = QObject::tr("%1<br>%2")
		.arg(u.getID())
		.arg(u.getCheckSum());
	return tipUser()
		.arg(VDRes::pathImg(u.m_state))
		.arg(u.getName())
		.arg(VDRes::text(u.m_state))
		.arg(s4);
}

const QString& VDStr::msgMaster()
{
	if(!s_isMsgMaster)
	{
		s_isMsgMaster = true;
		s_msgMaster = readFile("msg_master.html");
		if (s_msgMaster.isEmpty())
			s_msgMaster = "<img src='%1'><br>%2<br>%3<br>%4";

		s_msgMaster.replace(QObject::tr(replImgSrc), QObject::tr(replImgDest));
		s_msgMaster.replace(QObject::tr(replBGSrc), QObject::tr(replBGDest));
	}

	return s_msgMaster;
}

const QString& VDStr::msgUser()
{
	if(!s_isMsgUser)
	{
		s_isMsgUser = true;
		s_msgUser = readFile("msg_user.html");
		if (s_msgUser.isEmpty())
			s_msgUser = "<img src='%1'><br>%2<br>%3<br>%4";

		s_msgUser.replace(QObject::tr(replImgSrc), QObject::tr(replImgDest));
		s_msgUser.replace(QObject::tr(replBGSrc), QObject::tr(replBGDest));
	}

	return s_msgUser;
}

const QString& VDStr::labelUser()
{
	if(!s_isLabelUser)
	{
		s_isLabelUser = true;
		s_labelUser = readFile("label_user.html");
		if (s_labelUser.isEmpty())
			s_labelUser = "%1<br>%2";

		s_labelUser.replace(QObject::tr(replImgSrc), QObject::tr(replImgDest));
		s_labelUser.replace(QObject::tr(replBGSrc), QObject::tr(replBGDest));
	}

	return s_labelUser;
}

static const char* strClr[VDStr::MsgClrCount] = 
{
	"transparent",
	"green",
	"red"
};

QString VDStr::msgUser( const QString& mes, MsgClr msgClr )
{
	return msgUser()
		.arg(strClr[msgClr])
		.arg(mes);
}

QString VDStr::msgMaster( const QString& mes, MsgClr msgClr )
{
	return msgMaster()
		.arg(strClr[msgClr])
		.arg(mes);
}

QString VDStr::labelUser( const QString &name, const QTime &time )
{
	return labelUser()
		.arg(name)
		.arg(time.toString());
}